layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'table', 'laytpl', 'laydate'], function(){
    var $ = layui.$,
        table = layui.table,
        laydate = layui.laydate;

    laydate.render({
        elem: '#time', //指定元素
        type: 'datetime',
        range: true
    });

    var item = 'name';

    table.render({
        id: 'data-table',
        elem: '#data-table',
        url: 'audit/userInfo/list',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: '昵称更新审核列表',
        height: globalHeight,
        page: true,
        cols: [[
            {field: 'id', title: 'ID', align: 'center'},
            {field: 'userId', title: '用户id', align: 'center'},
            {field: 'host', title: '用户类型', align: 'center', templet: '#hostTpl'},
            {field: 'name', title: '昵称', align: 'center'},
            {field: 'createTime', title: '提交时间', align: 'center'},
            {field: 'status', title: '状态', align: 'center', templet: '#statusTpl'},
            {field: 'reason', title: '理由', align: 'center', templet:
                    function (d) {
                        if(d.reason != null){
                            return d.reason.replace(/\n/gi, "<br>");
                        }
                        return "";
                    }
            },
            {field: 'op', title: '操作', align: 'center', templet: '#data-table-bar'}
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.userInfoAudits //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        switch(obj.event){
            case 'search':
                var data =  getData();
                table.reload('data-table', {
                    url: 'audit/userInfo/list',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                reloadData(data);
                break;
        };
    });

    //监听行工具事件
    table.on('tool(data-table)', function(obj){
        var data = obj.data;
        if(obj.event === 'pass'){
            // 加载层
            var load = layerLoad();
            post('audit/userInfo/audit', {id: data.id, status: 1, item: item}, false, function () {
                obj.update({
                    status: 1,
                    op: 1
                });
                layer.close(load);
                layer.msg("审核通过");
            });
        } else if(obj.event === 'refuse'){
            layer.prompt({
                    title: '请输入拒绝理由',
                    offset: '25%',
                    area: ['100px', '50px']
                }, function(value, index, elem){
                    if(value == ''){
                        layer.msg("请输入拒绝理由");
                    }else{
                        layer.close(index);
                        // 加载层
                        var load = layerLoad();
                        post('audit/userInfo/audit', {id: data.id, status: 2, item: item, reason: value}, false, function () {
                            var reason = data.reason;
                            if(reason == null || reason == ''){
                                reason = value;
                            }else{
                                reason += "\n"+value;
                            }
                            obj.update({
                                status: 2,
                                reason: reason,
                                op: 2
                            });
                            layer.close(load);
                            layer.msg("拒绝成功");
                        });
                    }
                }
            );
        } else if(obj.event === 'cancelAudit'){
            layer.prompt({
                    title: '请输入撤销理由',
                    offset: '25%',
                    area: ['100px', '50px']
                }, function(value, index, elem){
                    if(value == ''){
                        layer.msg("请输入撤销理由");
                    }else{
                        layer.close(index);
                        // 加载层
                        var load = layerLoad();
                        post('audit/userInfo/cancelAudit', {id: data.id, item: item, reason: value}, false, function () {
                            var reason = data.reason;
                            if(reason == null || reason == ''){
                                reason = value;
                            }else{
                                reason += "\n"+value;
                            }
                            obj.update({
                                status: 0,
                                reason: reason,
                                op: 2
                            });
                            layer.close(load);
                            layer.msg("撤销成功");
                        });
                    }
                }
            );
        }
    });

    /**
     * 获取查询数据
     * @returns {{host: jQuery, online: jQuery, keyword: (*|jQuery|Array|string|undefined), status: (*|jQuery|Array|string|undefined)}}
     */
    function getData() {
        var userId = $('#userId').val();
        var status = $('#status').val();
        var time = $('#time').val();

        return {'item': item, 'userId': userId, 'status': status, 'createTime': time};
    }

    /**
     * 重新渲染数据
     * @param data
     */
    function reloadData(data) {
        $('#userId').val(data.userId);
        $('#status').val(data.status);
        $('#time').val(data.createTime);
        laydate.render({
            elem: '#time', //指定元素
            type: 'datetime',
            range: true
        });
    }
});